package com.dejian.bloodPressure;
import android.content.DialogInterface;
import android.content.Intent;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.RelativeLayout;
import com.sam.dialog.CustomAlertDialogBuilder;
import com.sam.gui.activity.SystemSettingsWebActivity;
import com.sam.ui.base.activity.BaseActivity;
import com.sam.widget.switcher.SwitchButton;
import chu.dejian.bloodPressure.R;
import chu.dejian.conf.BLEPreference;
import chu.dejian.restApi.RestClient;
import retrofit.Callback;
import retrofit.RetrofitError;
import retrofit.client.Response;

public class SettingsActivity extends BaseActivity {
    private View mFeedBackView;
    private View mAboutView;
    private RelativeLayout menuProfileSettings,menuReceiveNewNotify, menuSound, menuVibrate, menuUseSpeaker, menuProfile;
    private RelativeLayout menuFeedback, menuAbout;
    private SwitchButton switcherReceiveNewNotify, switcherSound, switcherVibrate, switcherUseSpeaker;
    private Button logoutBtn;

    @Override
    protected void onCreate(@Nullable Intent savedInstanceStateIntent) {
        setContentView(R.layout.activity_settings);
        findView();
        initView();
    }

    protected void findView() {
        menuProfileSettings = (RelativeLayout) findViewById(R.id.profile_settings);
        menuReceiveNewNotify = (RelativeLayout) findViewById(R.id.menu_receive_new_notify);
        menuSound = (RelativeLayout) findViewById(R.id.menu_sound);
        menuVibrate = (RelativeLayout) findViewById(R.id.menu_vibrate);
        menuUseSpeaker = (RelativeLayout) findViewById(R.id.menu_use_speaker);
        menuProfile = (RelativeLayout) findViewById(R.id.menu_profile);
        menuFeedback = (RelativeLayout) findViewById(R.id.menu_feedback);
        menuAbout = (RelativeLayout) findViewById(R.id.menu_about);
        switcherReceiveNewNotify = (SwitchButton) findViewById(R.id.switcher_receive_new_notify);
        switcherSound = (SwitchButton) findViewById(R.id.switcher_sound);
        switcherVibrate = (SwitchButton) findViewById(R.id.switcher_vibrate);
        switcherUseSpeaker = (SwitchButton) findViewById(R.id.switcher_use_speaker);
        logoutBtn = (Button) findViewById(R.id.btn_logout);
        mFeedBackView = findViewById(R.id.menu_feedback);
        mAboutView = findViewById(R.id.menu_about);
    }

    protected void initView() {
        menuProfileSettings.setOnClickListener(onClickListener);
        menuReceiveNewNotify.setOnClickListener(onClickListener);
        menuSound.setOnClickListener(onClickListener);
        menuVibrate.setOnClickListener(onClickListener);
        menuUseSpeaker.setOnClickListener(onClickListener);
        menuProfile.setOnClickListener(onClickListener);
        menuFeedback.setOnClickListener(onClickListener);
        menuAbout.setOnClickListener(onClickListener);
        logoutBtn.setOnClickListener(onClickListener);
        switcherReceiveNewNotify.setChecked(true);
        switcherSound.setChecked(true);
        switcherVibrate.setChecked(true);
        switcherUseSpeaker.setChecked(true);

        if (!TextUtils.isEmpty(BLEPreference.getInstance().getAccount(null))) {
            logoutBtn.setText(getString(R.string.logout) + "(" + BLEPreference.getInstance().getAccount("") + ")");
        }

        switcherReceiveNewNotify.setOnCheckChangeListener(onCheckedChangeListener);
        switcherSound.setOnCheckChangeListener(onCheckedChangeListener);
        switcherVibrate.setOnCheckChangeListener(onCheckedChangeListener);
        switcherUseSpeaker.setOnCheckChangeListener(onCheckedChangeListener);
        mFeedBackView.setOnClickListener(onClickListener);
        mAboutView.setOnClickListener(onClickListener);
    }


    // ===================================================================================
    //                                         事件定义
    // ===================================================================================
    protected final SwitchButton.OnCheckChangeListener onCheckedChangeListener = new SwitchButton.OnCheckChangeListener(){
        @Override
        public void OnCheck(SwitchButton switchButton, boolean isChecked) {
            switch (switchButton.getId()) {
                case R.id.profile_settings:
                    startActivity(new Intent(SettingsActivity.this, SystemSettingsWebActivity.class));
                    break;
                case R.id.switcher_receive_new_notify:
                    menuSound.setVisibility(isChecked ? View.VISIBLE : View.GONE);
                    menuVibrate.setVisibility(isChecked ? View.VISIBLE : View.GONE);
                    break;
                case R.id.switcher_sound:
                    break;
                case R.id.switcher_vibrate:
                    break;
                case R.id.switcher_use_speaker:
                    break;
                default:
                    break;
            }
        }
    };

    private OnClickListener onClickListener = new OnClickListener() {
        @Override
        public void onClick(View v) {
            switch (v.getId()) {
                case R.id.menu_receive_new_notify:
                    switcherReceiveNewNotify.setChecked(!switcherReceiveNewNotify.isChecked());
                    break;
                case R.id.menu_sound:
                    switcherSound.setChecked(!switcherSound.isChecked());
                    break;
                case R.id.menu_vibrate:
                    switcherVibrate.setChecked(!switcherVibrate.isChecked());
                    break;
                case R.id.menu_use_speaker:
                    switcherUseSpeaker.setChecked(!switcherUseSpeaker.isChecked());
                    break;
                case R.id.btn_logout: //退出登陆
                    showLogoutDialog();
                    break;
                case R.id.menu_about:
                    break;
                case R.id.menu_feedback:
                    break;
                case R.id.menu_profile:
                    break;
                default:
                    break;
            }
        }
    };


    public void showLogoutDialog() {
        new CustomAlertDialogBuilder.Builder(this).setTitle(getString(R.string.logout))
                .setMessage(getString(R.string.exit))
                .setPositiveButton(R.string.exit, new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                        logout();
                    }
                }).setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {

            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
            }
        }).create().show();
    }

    private void logout(){
        RestClient.getAuthedClient().getApiService().logout(RestClient.getAuthedClient().getToken(), new Callback() {
            @Override
            public void success(Object o, Response response) {
                BLEPreference.getInstance().setToken("");
            }

            @Override
            public void failure(RetrofitError retrofitError) {
            }
        });
    }
}
